package com.z.admin.controller;


import com.z.common.base.ResponseCode;
import com.z.common.base.Results;
import com.z.admin.service.IPermissionService;
import com.z.admin.service.IRolePermissionService;
import com.z.admin.vo.DTree;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author z
 * @since 2022-02-14
 */
@Controller
@RequestMapping("/admin/rolePermission")
@Api(tags = "角色授权相关")
public class RolePermissionController {

    @Autowired
    private IRolePermissionService rolePermissionService;
    @Autowired
    private IPermissionService permissionService;

    @GetMapping("/toList")
    @ApiOperation("角色授权列表页")
    public String toList(){
        return "admin/rolePermission/rolePermissionList";
    }

    @ResponseBody
    @PostMapping("/treeInfo")
    @ApiOperation("权限树")
    public Results treeInfo(String roleId){
        List<DTree> datas = permissionService.getTree(roleId);
        return Results.success(0,"查询成功", datas);
    }

    @PostMapping("save")
    @ApiOperation("角色权限保存接口")
    @ResponseBody
    public Results save(String ids, String roleId){

        try{
            if(StringUtils.isEmpty(roleId)){
                throw new Exception("参数有误");
            }

            rolePermissionService.saveByIds(ids, roleId);
        }catch (Exception e){
            e.printStackTrace();
            return Results.failure(ResponseCode.error.getCode(), e.getMessage());
        }
        return Results.success("保存成功");
    }
}
